<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%@ include file="../Connections/A146240Connection.jsp" %>
<%
// *** Edit Operations: declare variables

// set the form action variable
String MM_editAction = request.getRequestURI();
if (request.getQueryString() != null && request.getQueryString().length() > 0) {
  String queryString = request.getQueryString();
  String tempStr = "";
  for (int i=0; i < queryString.length(); i++) {
    if (queryString.charAt(i) == '<') tempStr = tempStr + "&lt;";
    else if (queryString.charAt(i) == '>') tempStr = tempStr + "&gt;";
    else if (queryString.charAt(i) == '"') tempStr = tempStr +  "&quot;";
    else tempStr = tempStr + queryString.charAt(i);
  }
  MM_editAction += "?" + tempStr;
}

// connection information
String MM_editDriver = null, MM_editConnection = null, MM_editUserName = null, MM_editPassword = null;

// redirect information
String MM_editRedirectUrl = null;

// query string to execute
StringBuffer MM_editQuery = null;

// boolean to abort record edit
boolean MM_abortEdit = false;

// table information
String MM_editTable = null, MM_editColumn = null, MM_recordId = null;

// form field information
String[] MM_fields = null, MM_columns = null;
%>
<%
// *** Insert Record: set variables

if (request.getParameter("MM_insert") != null && request.getParameter("MM_insert").toString().equals("form1")) {

  MM_editDriver     = MM_A146240Connection_DRIVER;
  MM_editConnection = MM_A146240Connection_STRING;
  MM_editUserName   = MM_A146240Connection_USERNAME;
  MM_editPassword   = MM_A146240Connection_PASSWORD;
  MM_editTable  = "A152786.ORDER_PRODUCT";
  MM_editRedirectUrl = "minusTotalQuantity.jsp";
  String MM_fieldsStr = "PRODUCT_ID|value|ORDER_ID|value|ORDER_AMOUNT|value";
  String MM_columnsStr = "PRODUCT_ID|none,none,NULL|ORDER_ID|',none,''|ORDER_AMOUNT|none,none,NULL";

  // create the MM_fields and MM_columns arrays
  java.util.StringTokenizer tokens = new java.util.StringTokenizer(MM_fieldsStr,"|");
  MM_fields = new String[tokens.countTokens()];
  for (int i=0; tokens.hasMoreTokens(); i++) MM_fields[i] = tokens.nextToken();

  tokens = new java.util.StringTokenizer(MM_columnsStr,"|");
  MM_columns = new String[tokens.countTokens()];
  for (int i=0; tokens.hasMoreTokens(); i++) MM_columns[i] = tokens.nextToken();

  // set the form values
  for (int i=0; i+1 < MM_fields.length; i+=2) {
    MM_fields[i+1] = ((request.getParameter(MM_fields[i])!=null)?(String)request.getParameter(MM_fields[i]):"");
  }
 
  // append the query string to the redirect URL
  if (MM_editRedirectUrl.length() != 0 && request.getQueryString() != null) {
    MM_editRedirectUrl += ((MM_editRedirectUrl.indexOf('?') == -1)?"?":"&") + request.getQueryString();
  }
}
%>
<%
// *** Insert Record: construct a sql insert statement and execute it

if (request.getParameter("MM_insert") != null) {

  // create the insert sql statement
  StringBuffer MM_tableValues = new StringBuffer(), MM_dbValues = new StringBuffer();
  for (int i=0; i+1 < MM_fields.length; i+=2) {
    String formVal = MM_fields[i+1];
    String elem;
    java.util.StringTokenizer tokens = new java.util.StringTokenizer(MM_columns[i+1],",");
    String delim    = ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:"";
    String altVal   = ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:"";
    String emptyVal = ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:"";
    if (formVal.length() == 0) {
      formVal = emptyVal;
    } else {
      if (altVal.length() != 0) {
        formVal = altVal;
      } else if (delim.compareTo("'") == 0) {  // escape quotes
        StringBuffer escQuotes = new StringBuffer(formVal);
        for (int j=0; j < escQuotes.length(); j++)
          if (escQuotes.charAt(j) == '\'') escQuotes.insert(j++,'\'');
        formVal = "'" + escQuotes + "'";
      } else {
        formVal = delim + formVal + delim;
      }
    }
    MM_tableValues.append((i!=0)?",":"").append(MM_columns[i]);
    MM_dbValues.append((i!=0)?",":"").append(formVal);
  }
  MM_editQuery = new StringBuffer("insert into " + MM_editTable);
  MM_editQuery.append(" (").append(MM_tableValues.toString()).append(") values (");
  MM_editQuery.append(MM_dbValues.toString()).append(")");
  
  if (!MM_abortEdit) {
    // finish the sql and execute it
    Driver MM_driver = (Driver)Class.forName(MM_editDriver).newInstance();
    Connection MM_connection = DriverManager.getConnection(MM_editConnection,MM_editUserName,MM_editPassword);
    PreparedStatement MM_editStatement = MM_connection.prepareStatement(MM_editQuery.toString());
    MM_editStatement.executeUpdate();
    MM_connection.close();

    // redirect with URL parameters
    if (MM_editRedirectUrl.length() != 0) {
		session.putValue("prodAmount", request.getParameter("ORDER_AMOUNT"));
		session.putValue("prodID", request.getParameter("PRODUCT_ID"));
      response.sendRedirect(response.encodeRedirectURL(MM_editRedirectUrl));
      return;
    }
  }
}
%>
<%
String getOrderID__MMColParam = "1";
if (session.getValue("MM_UserAuthorization") !=null) {getOrderID__MMColParam = (String)session.getValue("MM_UserAuthorization");}
%>
<%
String getOrderID__MMColParam2 = new java.text.SimpleDateFormat("dd.MM.yyyy").format(new java.util.Date());
if (session.getValue("currDate") !=null) {getOrderID__MMColParam2 = (String)session.getValue("currDate");}
%>
<%
Driver DrivergetOrderID = (Driver)Class.forName(MM_A146240Connection_DRIVER).newInstance();
Connection ConngetOrderID = DriverManager.getConnection(MM_A146240Connection_STRING,MM_A146240Connection_USERNAME,MM_A146240Connection_PASSWORD);
PreparedStatement StatementgetOrderID = ConngetOrderID.prepareStatement("SELECT ORDER_ID  FROM A152786.ORDER_BOOK  WHERE USER_ID = '" + getOrderID__MMColParam + "' AND ORDER_DATE = '" + getOrderID__MMColParam2 + "'");
ResultSet getOrderID = StatementgetOrderID.executeQuery();
boolean getOrderID_isEmpty = !getOrderID.next();
boolean getOrderID_hasData = !getOrderID_isEmpty;
Object getOrderID_data;
int getOrderID_numRows = 0;
String orderID="";
orderID =(((getOrderID_data = getOrderID.getObject("ORDER_ID"))==null || getOrderID.wasNull())?"":getOrderID_data).toString();
getOrderID.close();
StatementgetOrderID.close();
ConngetOrderID.close();
%>
<%
String getProductID__MMColParam = "1";
if (session.getAttribute("productAdd") !=null) {getProductID__MMColParam = (String)session.getAttribute("productAdd");}
%>
<%
Driver DrivergetProductID = (Driver)Class.forName(MM_A146240Connection_DRIVER).newInstance();
Connection ConngetProductID = DriverManager.getConnection(MM_A146240Connection_STRING,MM_A146240Connection_USERNAME,MM_A146240Connection_PASSWORD);
PreparedStatement StatementgetProductID = ConngetProductID.prepareStatement("SELECT PRODUCT_ID, PRODUCT_QUANTITY FROM A152786.PRODUCT WHERE PRODUCT_NAME = '" + getProductID__MMColParam + "'");
ResultSet getProductID = StatementgetProductID.executeQuery();
boolean getProductID_isEmpty = !getProductID.next();
boolean getProductID_hasData = !getProductID_isEmpty;
Object getProductID_data;
int getProductID_numRows = 0;
String prodID="";
String prodTotal="";
prodID=(((getProductID_data = getProductID.getObject("PRODUCT_ID"))==null || getProductID.wasNull())?"":getProductID_data).toString();
prodTotal=(((getProductID_data = getProductID.getObject("PRODUCT_QUANTITY"))==null || getProductID.wasNull())?"":getProductID_data).toString();
getProductID.close();
StatementgetProductID.close();
ConngetProductID.close();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<form method="post" action="<%=MM_editAction%>" name="form1">
  <table align="center">
    <tr valign="baseline">
      <td nowrap align="right">Product Name:</td>
      <td><input type="hidden" name="PRODUCT_ID" value="<%=prodID%>" size="32"><input type="text" name="PRODUCT_NAME" value="<%=session.getAttribute("productAdd")%>" size="32" readonly >
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Order ID:</td>
      <td><input type="text" name="ORDER_ID" value="<%=orderID%>" size="32" readonly >
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Order Amount:</td>
      <td><select name="ORDER_AMOUNT">
	  			<option value="1"  selected>1</option>
	  			<%for(int i=2;i<=Integer.parseInt(prodTotal);i++){%>
          		<option value="<%=i%>" ><%=i%></option>
				<%}%>
        </select>
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">&nbsp;</td>
      <td><input type="submit" value="Insert record">
      </td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1">
</form>
<p>&nbsp;</p>
</body>
</html>

